package at.ac.tuwien.big.form.htmlgen

import at.ac.tuwien.big.form.Form
import at.ac.tuwien.big.form.Heading
import at.ac.tuwien.big.form.InputField
import at.ac.tuwien.big.form.List
import at.ac.tuwien.big.form.Paragraph
import at.ac.tuwien.big.form.SelectionCondition
import at.ac.tuwien.big.form.SelectionField
import at.ac.tuwien.big.form.SelectionFieldType
import at.ac.tuwien.big.form.TextArea
import at.ac.tuwien.big.form.TextField
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import java.io.File

/*
 * open questions
 * is the resource set always in correct order (resp.. first welcome page.... a.s.o)
 */
class Form2HTMLGenerator implements IGenerator {
	
	//val fileName = "registration.html";

	override doGenerate(Resource resource, IFileSystemAccess fsa) {
		//iterating over all objects held by resources	
		for (eobj : resource.contents)
			{
				// generate an output file for form elements (should happen only once)
				if (eobj instanceof Form) {
					var out = eobj as Form;
				
					var f = new File(resource.URI.toString());
					var filename = f.name.replace(".".concat(resource.URI.fileExtension),".html");
					//make use of extension method generateHTML applied to local field out
					fsa.generateFile(filename, out.generateHTML)
				} 
			}
	}
	
	
	def generateHTML(Form form) 
		'''
		<!DOCTYPE html>
		<html lang="en">
			<head>
				<title>«form.welcomePage.title»</title>
				<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
				<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
				<link href="../assets/form.css" rel="stylesheet" media="screen"/>
				<script src="../assets/jquery-1.10.2.min.js" type="text/javascript"></script>
				<script src="../assets/form.js" type="text/javascript"></script>
				<script type="text/javascript">
				$(document).ready(
					function(){
						form.init();
						«{	var String out = '';
							for (page: form.pages) {
								for (element : page.elements) {
									out = out.concat(
										switch element {
											// Typeguard ..replaces instanceOf Condition in JAVA
											/* do not change format of  */
											InputField case element.mandatory : '''
											form.registerMandatory('«element.elementId»');
											'''
											InputField case !element.mandatory : '''
											form.registerOptional('«element.elementId»');
											'''
											default : ''''''
										}
									)
								}
							}
							out = out.concat(
							'''

							'''
							)
							for (page: form.pages) {
									for (element : page.elements) {
										out = out.concat(
											'''
											form.registerElement('«element.elementId»');
											'''
										)
									}
							}
							out = out.concat(
							'''

							'''
							)
							for (page: form.pages) {
									for (condition : page.visibilityConditions) {
									val cond = condition as SelectionCondition
									out = out.concat('''form.registerConditional('«cond.item.field.elementId»', '«cond.item.label»',
«FOR pe : cond.concernsElements BEFORE "['" SEPARATOR  "','" AFTER "']"»«pe.elementId»«ENDFOR»
);
									''')
								}	
							}
							out
						}»
					});
				</script>
			</head>
			<body>
				<div class="container">
««« IMPORTANT!!! PLEASE DO NOT CHANGE ANY INDENTS OR LINE BREAKS	
««« we can assume that the welcome page is always processed first	
					«FOR page : form.pages
BEFORE
'			<div class="page">
				<div class="well">
					<form action="#">
						<fieldset>'
SEPARATOR
'
						</fieldset>
					</form>
				</div>
			</div>
			<div class="page">
				<div class="well">
					<form action="#">
						<fieldset>'
AFTER
'						</fieldset>
					</form>
				</div>
			</div>'»
		
									«FOR pe : page.elements»
										«pe.extractPeInfo»
									«ENDFOR»
«ENDFOR»

				</div>
			</body>
		</html>
		'''
	
def dispatch extractPeInfo(Heading heading) '''
	<h«heading.level» id="«heading.elementId»">«heading.content»</h«heading.level»>
'''

def dispatch extractPeInfo(Paragraph paragraph) '''
	<p id="«paragraph.elementId»">«paragraph.content»</p>
'''

def dispatch extractPeInfo(List list) '''
	«IF list.ordered»
	<ol id="«list.elementId»">
		«FOR bullet : list.items»
		<li>«bullet.label»</li>
		«ENDFOR»
	</ol>
	«ENDIF»
	«IF !list.ordered»
	<ul id="«list.elementId»">
		«FOR bullet : list.items»
		<li>«bullet.label»</li>
		«ENDFOR»
	</ul>
	«ENDIF»
'''

def dispatch extractPeInfo(TextField tfield) '''
	<div class="control-group">
		<label for="«tfield.elementId»">«tfield.label»</label>
		<div class="controls">
			«IF tfield.encrypted»
				<input type="password" id="«tfield.elementId»"/>
			«ELSE»
				<input type="text" id="«tfield.elementId»"/>
			«ENDIF»
		</div>
	</div>'''

def dispatch extractPeInfo(TextArea tarea) '''
	<div class="control-group">
		<label for="«tarea.elementId»">«tarea.label»</label>
		<div class="controls">
			<textarea id="«tarea.elementId»"></textarea>
		</div>
	</div>'''

def dispatch extractPeInfo(SelectionField selfield) '''
	<div class="control-group">
		«IF (selfield.selectionFieldType == SelectionFieldType.COMBOBOX)»
			<label for="«selfield.elementId»" class="lone">«selfield.label»</label>
			<div class="controls">
				<select id="«selfield.elementId»">
					«FOR item : selfield.items»
						<option value="«item.label»"«IF item.selected» selected="selected"«ENDIF»>«item.label»</option>
					«ENDFOR»
				</select>
			</div>
		«ELSE»
		<label class="lone" id="label_for_«selfield.elementId»">«selfield.label»</label>
		<div class="controls" id="«selfield.elementId»">
			«{	
			var i = -1; //counter to label selectionitem id
			switch selfield.selectionFieldType {
				case SelectionFieldType.RADIO : '''
					«FOR item : selfield.items»
					<label class="radio" for="«selfield.elementId»_«{i=i+1}»">
						<input type="radio" value="«item.label»" name="«selfield.elementId»" id="«selfield.elementId»_«i»"«IF item.selected» checked="checked"«ENDIF» /> «item.label»
					</label>
					«ENDFOR»
					'''	
				case SelectionFieldType.CHECKBOX : '''				
					«FOR item : selfield.items»
					<label class="checkbox" for="«selfield.elementId»_«{i=i+1}»">
						<input type="checkbox" value="«item.label»" name="«selfield.elementId»" id="«selfield.elementId»_«i»" «IF item.selected»checked="checked"«ENDIF»/> «item.label»
					</label>
					«ENDFOR»
					'''}}»
		</div>
«ENDIF»
	</div>
'''
}
